import ahocorasick

def search_with_pyahocorasick(text, patterns):
    A = ahocorasick.Automaton()
    for pattern in patterns:
        A.add_word(pattern, pattern)

    A.make_automaton()

    matches = []
    for item in A.iter(text):
        end_index = item[0]
        pattern = item[1]
        start_index = end_index - len(pattern) + 1
        matches.append((pattern, (start_index, end_index)))
    data = list(set(matches))
    result = []
    for item in data:
        tempObj = {
            "patternString": item[0],
            "matchStartIndex": item[1][0],
            "matchEndIndex": item[1][1]
        }
        result.append(tempObj)
    sortedResult = sorted(result, key=lambda x: (x['matchEndIndex'], x['matchStartIndex']))
    return sortedResult

# Example
text = "abccab"
patterns = ["ab", "bc", "ca", "ccab"]
result = search_with_pyahocorasick(text, patterns)
for item in result:
    print(item)
